home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / gaplus.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  11KB  |  413 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12. /***************************************************************************
  13.  
  14.   Convert the color PROMs into a more useable format.
  15.  
  16.   The palette PROMs are connected to the RGB output this way:
  17.  
  18.   bit 3 -- 220 ohm resistor  -- RED/GREEN/BLUE
  19.         -- 470 ohm resistor  -- RED/GREEN/BLUE
  20.         -- 1  kohm resistor  -- RED/GREEN/BLUE
  21.   bit 0 -- 2.2kohm resistor  -- RED/GREEN/BLUE
  22.  
  23. ***************************************************************************/
  24. void gaplus_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  25. {
  26.     int i;
  27.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  28.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  29.  
  30.  
  31.     for (i = 0;i < Machine->drv->total_colors;i++)
  32.     {
  33.         int bit0,bit1,bit2,bit3;
  34.  
  35.  
  36.         /* red component */
  37.         bit0 = (color_prom[0] >> 0) & 0x01;
  38.         bit1 = (color_prom[0] >> 1) & 0x01;
  39.         bit2 = (color_prom[0] >> 2) & 0x01;
  40.         bit3 = (color_prom[0] >> 3) & 0x01;
  41.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  42.         /* green component */
  43.         bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  44.         bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  45.         bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  46.         bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  47.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  48.         /* blue component */
  49.         bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  50.         bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  51.         bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  52.         bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  53.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  54.  
  55.         color_prom++;
  56.     }
  57.  
  58.     color_prom += 2*Machine->drv->total_colors;
  59.     /* color_prom now points to the beginning of the lookup table */
  60.  
  61.  
  62.     /* characters use colors 240-255 */
  63.     for (i = 0;i < TOTAL_COLORS(0);i++)
  64.         COLOR(0,i) = 240 + (*(color_prom++) & 0x0f);
  65.  
  66.     /* sprites */
  67.     for (i = 0;i < TOTAL_COLORS(2);i++)
  68.     {
  69.         COLOR(2,i) = (color_prom[0] & 0x0f) + ((color_prom[TOTAL_COLORS(2)] & 0x0f) << 4);
  70.         color_prom++;
  71.     }
  72. }
  73.  
  74. /***************************************************************************
  75.     Starfield information
  76.     There's 3 sets of stars planes at different speeds.
  77.  
  78.     a000 ---> (bit 0 = 1) enable starfield.
  79.                 (bit 0 = 0) disable starfield.
  80.     a001 ---> starfield plane 0 control
  81.     a002 ---> starfield plane 1 control
  82.     a003 ---> starfield plane 2 control
  83. ***************************************************************************/
  84.  
  85. #define MAX_STARS            250
  86.  
  87. /* starfield speed constants (bigger = faster) */
  88. #define SPEED_1 0.5
  89. #define SPEED_2 1.0
  90. #define SPEED_3 2.0
  91.  
  92. struct star {
  93.     float x,y;
  94.     int col,set;
  95. };
  96. static struct star stars[MAX_STARS];
  97.  
  98. static unsigned char gaplus_starfield_control[4];
  99. static int total_stars;
  100.  
  101. static void starfield_init( void ) {
  102.  
  103.     int generator = 0;
  104.     int x,y;
  105.     int set = 0;
  106.     int width, height;
  107.  
  108.     width = Machine->drv->screen_width;
  109.     height = Machine->drv->screen_height;
  110.  
  111.     total_stars = 0;
  112.  
  113.     /* precalculate the star background */
  114.     /* this comes from the Galaxian hardware, Gaplus is probably different */
  115.  
  116.     for ( y = 0;y < height; y++ ) {
  117.         for ( x = width*2 - 1; x >= 0; x--) {
  118.             int bit1,bit2;
  119.  
  120.             generator <<= 1;
  121.             bit1 = (~generator >> 17) & 1;
  122.             bit2 = (generator >> 5) & 1;
  123.  
  124.             if (bit1 ^ bit2) generator |= 1;
  125.  
  126.             if ( ((~generator >> 16) & 1) && (generator & 0xff) == 0xff) {
  127.                 int color;
  128.  
  129.                 color = (~(generator >> 8)) & 0x3f;
  130.                 if ( color && total_stars < MAX_STARS ) {
  131.                     stars[total_stars].x = x;
  132.                     stars[total_stars].y = y;
  133.                     stars[total_stars].col = Machine->pens[color];
  134.                     stars[total_stars].set = set++;
  135.  
  136.                     if ( set == 3 )
  137.                         set = 0;
  138.  
  139.                     total_stars++;
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }
  145.  
  146. void gaplus_starfield_update( void ) {
  147.     int i;
  148.     int width, height;
  149.  
  150.     width = Machine->drv->screen_width;
  151.     height = Machine->drv->screen_height;
  152.  
  153.     /* check if we're running */
  154.     if ( ( gaplus_starfield_control[0] & 1 ) == 0 )
  155.         return;
  156.  
  157.     /* update the starfields */
  158.     for ( i = 0; i < total_stars; i++ ) {
  159.         switch( gaplus_starfield_control[stars[i].set + 1] ) {
  160.             case 0x87:
  161.                 /* stand still */
  162.             break;
  163.  
  164.             case 0x86:
  165.                 /* scroll down (speed 1) */
  166.                 stars[i].x += SPEED_1;
  167.             break;
  168.  
  169.             case 0x85:
  170.                 /* scroll down (speed 2) */
  171.                 stars[i].x += SPEED_2;
  172.             break;
  173.  
  174.             case 0x06:
  175.                 /* scroll down (speed 3) */
  176.                 stars[i].x += SPEED_3;
  177.             break;
  178.  
  179.             case 0x80:
  180.                 /* scroll up (speed 1) */
  181.                 stars[i].x -= SPEED_1;
  182.             break;
  183.  
  184.             case 0x82:
  185.                 /* scroll up (speed 2) */
  186.                 stars[i].x -= SPEED_2;
  187.             break;
  188.  
  189.             case 0x81:
  190.                 /* scroll up (speed 3) */
  191.                 stars[i].x -= SPEED_3;
  192.             break;
  193.  
  194.             case 0x9f:
  195.                 /* scroll left (speed 2) */
  196.                 stars[i].y += SPEED_2;
  197.             break;
  198.  
  199.             case 0xaf:
  200.                 /* scroll left (speed 1) */
  201.                 stars[i].y += SPEED_1;
  202.             break;
  203.         }
  204.  
  205.         /* wrap */
  206.         if ( stars[i].x < 0 )
  207.             stars[i].x = ( float )( width*2 ) + stars[i].x;
  208.  
  209.         if ( stars[i].x >= ( float )( width*2 ) )
  210.             stars[i].x -= ( float )( width*2 );
  211.  
  212.         if ( stars[i].y < 0 )
  213.             stars[i].y = ( float )( height ) + stars[i].y;
  214.  
  215.         if ( stars[i].y >= ( float )( height ) )
  216.             stars[i].y -= ( float )( height );
  217.     }
  218. }
  219.  
  220. static void starfield_render( struct osd_bitmap *bitmap ) {
  221.  
  222.     int i;
  223.     int width, height;
  224.  
  225.     width = Machine->drv->screen_width;
  226.     height = Machine->drv->screen_height;
  227.  
  228.     /* check if we're running */
  229.     if ( ( gaplus_starfield_control[0] & 1 ) == 0 )
  230.         return;
  231.  
  232.     /* draw the starfields */
  233.     for ( i = 0; i < total_stars; i++ )
  234.     {
  235.         int x, y;
  236.  
  237.         x = stars[i].x;
  238.         y = stars[i].y;
  239.  
  240.         if ( x >=0 && x < width && y >= 0 && y < height )
  241.         {
  242.             plot_pixel(bitmap, x, y, stars[i].col);
  243.         }
  244.     }
  245. }
  246.  
  247. WRITE_HANDLER( gaplus_starfield_control_w ) {
  248.     gaplus_starfield_control[offset] = data;
  249. }
  250.  
  251. static int flipscreen = 0;
  252.  
  253. void gaplus_flipscreen_w( int data )
  254. {
  255.     flipscreen = data;
  256. }
  257.  
  258. extern unsigned char *gaplus_sharedram;
  259.  
  260. int gaplus_vh_start( void ) {
  261.  
  262.     /* set up spriteram area */
  263.     spriteram_size = 0x80;
  264.     spriteram = &gaplus_sharedram[0x780];
  265.     spriteram_2 = &gaplus_sharedram[0x780+0x800];
  266.     spriteram_3 = &gaplus_sharedram[0x780+0x800+0x800];
  267.  
  268.     starfield_init();
  269.  
  270.     return generic_vh_start();
  271. }
  272.  
  273. void gaplus_vh_stop( void ) {
  274.  
  275.     generic_vh_stop();
  276. }
  277.  
  278. /***************************************************************************
  279.  
  280.     Display Refresh
  281.  
  282. ***************************************************************************/
  283.  
  284. static void gaplus_draw_sprites(struct osd_bitmap *bitmap){
  285.     int offs;
  286.  
  287.     for (offs = 0; offs < spriteram_size; offs += 2){
  288.         if ((spriteram_3[offs+1] & 2) == 0){
  289.             int number = spriteram[offs]+4*(spriteram_3[offs] & 0x40);
  290.             int color = spriteram[offs+1] & 0x3f;
  291.             int sx = (spriteram_2[offs+1]-71) + 0x100*(spriteram_3[offs+1] & 1);
  292.             int sy = ( Machine->drv->screen_height ) - spriteram_2[offs]-24;
  293.             int flipy = spriteram_3[offs] & 2;
  294.             int flipx = spriteram_3[offs] & 1;
  295.             int width, height;
  296.  
  297.             if (number >= 128*3) continue;
  298.  
  299.             if (flipscreen)
  300.             {
  301.                 flipx = !flipx;
  302.                 flipy = !flipy;
  303.             }
  304.  
  305.             if ((spriteram_3[offs] & 0xa8) == 0xa0){ /* draw the sprite twice in a row */
  306.                     drawgfx(bitmap,Machine->gfx[2+(number >> 7)],
  307.                                 number,color,flipx,flipy,sx,sy,
  308.                                 &Machine->drv->visible_area,TRANSPARENCY_COLOR,255);
  309.                     drawgfx(bitmap,Machine->gfx[2+(number >> 7)],
  310.                                 number,color,flipx,flipy,sx,sy+16,
  311.                                 &Machine->drv->visible_area,TRANSPARENCY_COLOR,255);
  312.             }
  313.             else{
  314.                 switch (spriteram_3[offs] & 0x28){
  315.                     case 0x28:    /* 2x both ways */
  316.                         width = height = 2; number &= (~3); break;
  317.                     case 0x20:    /* 2x vertical */
  318.                         width = 1; height = 2; number &= (~2); break;
  319.                     case 0x08:    /* 2x horizontal */
  320.                         width = 2; height = 1; number &= (~1); sy += 16; break;
  321.                     default:    /* normal sprite */
  322.                         width = height = 1; sy += 16; break;
  323.                 }
  324.                 {
  325.                     static int x_offset[2] = { 0x00, 0x01 };
  326.                     static int y_offset[2] = { 0x00, 0x02 };
  327.                     int x,y, ex, ey;
  328.  
  329.                     for( y=0; y < height; y++ ){
  330.                         for( x=0; x < width; x++ ){
  331.                             ex = flipx ? (width-1-x) : x;
  332.                             ey = flipy ? (height-1-y) : y;
  333.  
  334.                             drawgfx(bitmap,Machine->gfx[2+(number >> 7)],
  335.                                 (number)+x_offset[ex]+y_offset[ey],
  336.                                 color,
  337.                                 flipx, flipy,
  338.                                 sx+x*16,sy+y*16,
  339.                                 &Machine->drv->visible_area,
  340.                                 TRANSPARENCY_COLOR,255);
  341.                         }
  342.                     }
  343.                 }
  344.             }
  345.         }
  346.     }
  347. }
  348.  
  349. void gaplus_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  350. {
  351.     int offs;
  352.  
  353.     fillbitmap( bitmap, Machine->pens[0], &Machine->drv->visible_area );
  354.  
  355.     starfield_render( bitmap );
  356.  
  357.     /* colorram layout: */
  358.     /* bit 7 = bank */
  359.     /* bit 6 = not used? */
  360.     /* bit 5-0 = color */
  361.  
  362.  
  363.     /* for every character in the Video RAM, check if it has been modified */
  364.     /* since last time and update it accordingly. */
  365.     for (offs = videoram_size - 1;offs >= 0;offs--)
  366.     {
  367.         int sx,sy,mx,my, bank;
  368.  
  369.         /* Even if Gaplus screen is 28x36, the memory layout is 32x32. We therefore
  370.         have to convert the memory coordinates into screen coordinates.
  371.         Note that 32*32 = 1024, while 28*36 = 1008: therefore 16 bytes of Video RAM
  372.         don't map to a screen position. We don't check that here, however: range
  373.         checking is performed by drawgfx(). */
  374.  
  375.         mx = offs / 32;
  376.         my = offs % 32;
  377.  
  378.         if (mx <= 1)        /* bottom screen characters */
  379.         {
  380.             sx = 29 - my;
  381.             sy = mx + 34;
  382.         }
  383.         else if (mx >= 30)  /* top screen characters */
  384.         {
  385.             sx = 29 - my;
  386.             sy = mx - 30;
  387.         }
  388.         else                /* middle screen characters */
  389.         {
  390.             sx = 29 - mx;
  391.             sy = my + 2;
  392.         }
  393.  
  394.         if (flipscreen)
  395.         {
  396.             sx = 27 - sx;
  397.             sy = 35 - sy;
  398.         }
  399.  
  400.         sx = ( ( Machine->drv->screen_height - 1 ) / 8 ) - sx;
  401.  
  402.         bank = ( colorram[offs] & 0x80 ) ? 1 : 0;
  403.  
  404.         drawgfx(bitmap,Machine->gfx[bank],
  405.                 videoram[offs],
  406.                 colorram[offs] & 0x3f,
  407.                 flipscreen,flipscreen,8*sy,8*sx,
  408.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  409.     }
  410.  
  411.     gaplus_draw_sprites(bitmap);
  412. }
  413.